package com.xtaller.test;

import com.xtaller.test.model.BaseUser;
import lombok.var;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-08-15
 * @Description:
 */
public class Fun {
    /**
     * 代表没有输入只有一个输出
     * */
    public static Supplier getAge1 = () -> 11;
    public static Supplier getAge2 = () -> {
        int a = 1;
        return "age=" + a;
    };

    public static Supplier getUsers = () -> {
        var users = new ArrayList<BaseUser>();
        for(int i=0; i<10; i++){
            var user = new BaseUser();
            user.setId((long) (i+1));
            user.setName("zs"+i);
            user.setAge(10+i);
            users.add(user);
        }
        return users;
    };
    /**
     * 代表只有一个输入不输出 (不常用)
     * */
    public static Consumer log = key -> {
        System.err.println(key.getClass().getTypeName());
    };

    /**
     * 代表只有2个输入不输出 (不常用)
     * */
    public static BiConsumer logs = (key, val) -> {
        System.err.println(key + "=" + val);
    };

    /**
     * 代表只有一个输入一输出
     * */
    public static Function<Integer, Integer> sum = key -> 1 + key;

    public static BiFunction<Integer, Integer, String> sums = (a, b) -> {
        return "sum=" + (a + b);
    };



    public static void main(String[] args){
        var user11 = new BaseUser();
        user11.setId(1L);
        var user12 = new BaseUser();
        user12.setId(1L);

        System.out.println(user11 == user12);

        var array = Arrays.asList("a", "b", "c", "c", "d", "e" ,"a");
//        System.err.println(getAge2.get());
//        log.accept("asdasdasd");
//        var user = new User();
//        user.setAge(11);
//        user.setName("zs");
//        log.accept(user);

        logs.accept("age", 11);
        // 取出id = 5
        var user = ((List<BaseUser>)getUsers.get())
                .stream()
                .filter(x -> x.getId().longValue() <= 5L)
                .collect(Collectors.toList());
        //
        // 如果我不知道user有没有
        if(user.size() > 0){
            System.err.println(user);
        }else {
            System.err.println("找不到");
        }


        // 取出 id <6 年龄在 10 - 12 之间
        var user2 = ((List<BaseUser>)getUsers.get())
                .stream()
                .filter(x -> x.getId().longValue() <= 5L)
                .filter(x -> x.getAge() >= 10)
                .filter(x -> x.getAge() <= 12)
                .collect(Collectors.toList());
        if(user2.size() > 0){
            System.err.println(user2);
        }else {
            System.err.println("找不到");
        }

        var user3 = ((List<BaseUser>)getUsers.get());
        var user4 = user2.stream()
                .sorted((x, y) -> y.getAge() - x.getAge())
                .collect(Collectors.toList());
        System.err.println(user4);

        var array2 = array.stream().distinct().collect(Collectors.toList());


    }




    public static int getAge(){
        return 10;
    }
}
